﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Asteriods
{
    public class Input : GameComponent
    {
        private KeyboardState currentKeyboardState = Keyboard.GetState();
        private KeyboardState previousKeyboardState = Keyboard.GetState();
        private MouseState currentMouseState = Mouse.GetState();
        public MouseState MouseState
        {
            get { return currentMouseState; }
        }
        private MouseState previousMouseState = Mouse.GetState();
        private Vector2 currentMousePosition = Vector2.Zero;
        public Vector2 MousePosition
        {
            get { return currentMousePosition; }
        }
        private Vector2 previousmouseposition = Vector2.Zero;
        public Vector2 PreviousMousePosition
        {
            get { return previousmouseposition; }
        }
        private int viewportWidth = 0;
        public int ViewportWidth
        {
            get { return viewportWidth; }
        }
        private int viewportHeight = 0;
        public int ViewportHeight
        {
            get { return viewportHeight; }
        }
        public Input(Game game) : base(game)
        {
            viewportWidth = game.GraphicsDevice.Viewport.Width;
            viewportHeight = game.GraphicsDevice.Viewport.Height;
        }
        public override void Initialize()
        {
            base.Initialize();
        }
        public override void Update(GameTime gameTime)
        {
            previousKeyboardState = currentKeyboardState;
            previousMouseState = currentMouseState;
            currentMouseState = Mouse.GetState();
            currentKeyboardState = Keyboard.GetState();
            
            currentMousePosition = new Vector2(currentMouseState.X, currentMouseState.Y);
            currentMousePosition.X = MathHelper.Clamp(currentMousePosition.X, (viewportWidth / 2) - 100, (viewportWidth / 2) + 100);
            currentMousePosition.Y = MathHelper.Clamp(currentMousePosition.Y, (viewportHeight / 2) - 100, (viewportHeight / 2) + 100);
            Mouse.SetPosition((int)currentMousePosition.X, (int)currentMousePosition.Y);
            previousmouseposition = currentMousePosition;
            base.Update(gameTime);
        }
        public virtual bool SingleKeyPress(Keys key)
        {
            if (currentKeyboardState.IsKeyDown(key) && previousKeyboardState.IsKeyUp(key))
                return true;
            else
                return false;
        }
        public virtual bool HoldKeyDown(Keys key)
        {
            if (currentKeyboardState.IsKeyDown(key) && previousKeyboardState.IsKeyUp(key))
                return true;
            else
                return false;
        }
        public virtual bool HoldKeyUp(Keys key)
        {
            if (currentKeyboardState.IsKeyUp(key) && previousKeyboardState.IsKeyUp(key))
                return true;
            else
                return false;
        }
        public virtual bool HoldLeftClick()
        {
            if (currentMouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton == ButtonState.Pressed)
                return true;
            else
                return false;
        }
        public virtual bool HoldRightClick()
        {
            if (currentMouseState.RightButton == ButtonState.Pressed && previousMouseState.RightButton == ButtonState.Pressed)
                return true;
            else
                return false;
        }
        public virtual bool HoldMiddleClick()
        {
            if (currentMouseState.MiddleButton == ButtonState.Pressed && previousMouseState.MiddleButton == ButtonState.Pressed)
                return true;
            else
                return false;
        }
        public virtual bool SingleMiddleClick()
        {
            if (currentMouseState.MiddleButton == ButtonState.Pressed &&
                previousMouseState.MiddleButton == ButtonState.Released)
                return true;
            else
                return false;
        }
        public virtual bool SingleLeftClick()
        {
            if (currentMouseState.LeftButton == ButtonState.Pressed &&
                previousMouseState.LeftButton == ButtonState.Released)
                return true;
            else
                return false;           
        }
        public virtual bool SingleRightKey()
        {
            if (currentMouseState.RightButton == ButtonState.Pressed &&
                previousMouseState.RightButton == ButtonState.Released)
                return true;
            else
                return false;
        }
    }
}
